﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vara.Person;
using System.Data;
using XentrixModules;
using System.Collections.Specialized;


public partial class User_Controls_Modal_SearchHolder : System.Web.UI.UserControl
{
    public delegate void PersonSelectionEventHandler(object sender, PersonSelectionArgs e);
    public event PersonSelectionEventHandler PersonSelected;
    PersonController Controller = new PersonController();

    protected void XRCSearchPersonOuter_Closing(object sender, EventArgs e)
    {
        if (Closed != null)
        {
            Closed(this, e);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Reusable.DetectEnterKey(XRCBSearchPerson, TextBoxFirstName);
        Reusable.DetectEnterKey(XRCBSearchPerson, TextBoxLastName);
        Reusable.DetectEnterKey(XRCBSearchPerson, TextBoxPersonReference);
        //AaronTestBindGridView();
    }
    public void Show()
    {
        TextBoxFirstName.Text = string.Empty;
        TextBoxLastName.Text = string.Empty;
        TextBoxPersonReference.Text = string.Empty;
        GridViewSearchResults.DataBind();
        XRCSearchPersonOuter.Show();
    }

    protected void XRCBSearchPerson_Click(object sender, EventArgs e)
    {

        GridViewSearchResults.DataSource = Controller.Search_People(TextBoxFirstName.Text, TextBoxLastName.Text, TextBoxPersonReference.Text, CheckBoxLookingStaff.Checked, CheckBoxLookingStudent.Checked, CheckBoxLookingExternalPerson.Checked);
        GridViewSearchResults.DataBind();
    }

    protected void ButtonAddNewClient_Click(object sender, EventArgs e)
    {
    }

    protected void GridViewClientSearchResults_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        IOrderedDictionary keys = GridViewSearchResults.DataKeys[e.NewSelectedIndex].Values;
        TriggerSelectionEvent(sender, keys[0].ToString(), keys[1].ToString());
        this.Hide();
    }
    private void TriggerSelectionEvent(object sender, string PersonReference, string personType)
    {
        IPerson person = this.Controller.ReturnPerson(PersonReference, personType);
        PersonSelectionArgs ArgsToSend = new PersonSelectionArgs();
        ArgsToSend.PersonName = person.ToString();
        ArgsToSend.PersonReference = PersonReference;
        ArgsToSend.PersonType = personType;

        if (PersonSelected != null)
        {
            PersonSelected(sender, ArgsToSend);
        }
        else
        {
            throw new Exception("Silly fool implement the 'OnPersonSelectedEvent!'");
        }
    }

    public class PersonSelectionArgs : EventArgs
    {
        public string PersonName;
        public string PersonReference;
        public string PersonType;
    }

    public event EventHandler Closed;

    public void ClearFields()
    {
        throw new NotImplementedException();
    }

    public void Hide()
    {
        XRCSearchPersonOuter.Hide();
    }

    protected void AddMissingPerson_Click(object sender, EventArgs e)
    {
        XRCAddNewExternalPerson.Show();
    }

    protected void XRCBAddNewExternal_Click(object sender, EventArgs e)
    {
        HideXRCSearchPerson();

        XRCAddNewExternalPerson.Show();

        Stranger NewPerson = new Stranger();
        NewPerson.Lastname = TextBoxAddLastName.Text;
        NewPerson.Firstname = TextBoxAddFirstName.Text;
        NewPerson.Address1 = TextBoxAddress1.Text;
        NewPerson.Address2 = TextBoxAddress2.Text;
        NewPerson.Address3 = TextBoxAddress3.Text;
        NewPerson.Postcode = TextBoxPostcode.Text;
        NewPerson.TelephoneNumber = TextBoxTelephone.Text;
        NewPerson.EmailAddress = TextBoxEmail.Text;

        PersonController PersonController = new PersonController();
        PersonController.InsertPerson(NewPerson);

        XRCAddNewExternalPerson.Hide();

        ShowXRCSearchPerson();
    }

    protected void ShowXRCSearchPerson()
    {
        XRCSearchPersonOuter.Show();
        XRCSearchPersonInner.Show();
        XRCInnerSearchInput.Show();
        XRCInnerSearchResults.Show();
    }

    protected void HideXRCSearchPerson()
    {
        XRCSearchPersonOuter.Hide();
        XRCSearchPersonInner.Hide();
        XRCInnerSearchInput.Hide();
        XRCInnerSearchResults.Hide();
    }
}